//
//  MyAssetViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/24.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MyAssetViewController.h"
#import "MyAssetHeaderView.h"
#import "MyAssetCell.h"
#import "TransicationDetailViewController.h"
#import "TopUpViewController.h"
#import "WithdrawViewController.h"
#import "RealNameViewController.h"
#import "MyAdoptedViewController.h"
#import "DiscountCouponViewController.h"
#import "WithdrawDetailViewController.h"


@interface MyAssetViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTbaleView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 headerView
 */
@property (nonatomic , strong) MyAssetHeaderView *headerView;
/**
 model
 */
@property (nonatomic , strong) MyAsset *model;


@end


@implementation MyAssetViewController


#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpView];
    [self setUpNav];
}


#pragma mark - init method
- (void)setUpNav {
   
    {
        self.baseNavView.navTitleLabel.text = @"我的资产";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        self.baseNavView.backgroundColor = KClearColor;
        self.baseNavView.line.hidden = YES;
    }
    
    {
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_whitev"] forState:UIControlStateNormal];
        [self.myTableView addSubview:self.baseNavView];
    }

    {
        [self.baseNavView.rightNavBtn setTitle:@"明细" forState:UIControlStateNormal];
        [self.baseNavView.rightNavBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        self.baseNavView.rightNavBtn.size = CGSizeMake(60, 32);
        self.baseNavView.rightNavBtn.mj_x = KScreenWidth -60 ;
        self.baseNavView.rightNavBtn.mj_y = TJ_StatusBarHeight + 3;
        [self.baseNavView.rightNavBtn setBackgroundImage:[UIImage imageNamed:@"user_myasset_transicationdetail"] forState:UIControlStateNormal];
        
        @weakify(self);
        self.baseNavView.rightBlock = ^{
            @strongify(self);
            [self transicationDetailClick];
        };
    }
}


- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.myTableView];
}


- (void) initLayout {
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(- TJ_StatusBarHeight, 0, 0, 0));
    }];
}


- (void) initAction {
    
    [self.headerView.withdrawBtn addTarget:self action:@selector(withdrawBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.topUpBtn addTarget:self action:@selector(topUpBtnClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)initData {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] getMyAssetWithMemberId:memberId] subscribeNext:^(MyAsset * _Nullable myAsset) {
        @strongify(self);
        self.model = [[MyAsset alloc] init];
        self.model = myAsset;
        self.headerView.balance.text = [NSString stringWithFormat:@"￥%@",self.model.money];
        [kUserDefaults setObject:self.model.money forKey:kUserPrice];
        [kUserDefaults synchronize];

        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}



#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyAssetCell *cell = [MyAssetCell cellWithTableView:tableView];
    [cell configCellWithModel:self.model index:indexPath];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0: //预售款
            {
                WithdrawDetailViewController *vc = [[WithdrawDetailViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 1: //已领养
            {
                MyAdoptedViewController *vc = [[MyAdoptedViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 2: //豚券
            {
                DiscountCouponViewController *vc = [[DiscountCouponViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 3: //买肉
            {
        
            }
            break;
      default: //奖金
            {
            
            }
            break;
    }
}


#pragma mark - target action

//明细
- (void)transicationDetailClick {
    
    TransicationDetailViewController *vc = [[TransicationDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


//提现
- (void)withdrawBtnClick {
    
    [self toastWithContent:@"测试临时关闭实名认证校验功能"];
    self.model.attestation = @(IsRealNameStatusYes);
    
    @weakify(self);
    if ([self.model.attestation integerValue] == IsRealNameStatusNo) {
        
        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"您未实名认证，为了您的资产安全，需实名认证可以提现" confirmBtnTitle:@"去实名认证" confirmBlock:^{
            @strongify(self);
            RealNameViewController *vc = [[RealNameViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];

        }cancelBlock: nil];

    } else if ([self.model.attestation integerValue] == IsRealNameStatusYes){
        WithdrawViewController *vc = [[WithdrawViewController alloc] init];
        vc.balance = self.model.money;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


//充值                                                        
- (void)topUpBtnClick {
    
    TopUpViewController *vc = [[TopUpViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


#pragma mark - getter and setter
- (UITableView *)myTableView {
    
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 115;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.tableHeaderView = self.headerView;
    }
    return _myTableView;
}


- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


- (MyAssetHeaderView *)headerView {
    
    if (!_headerView) {
        _headerView = [[MyAssetHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * [JTool getScreenWidthScale])];
    }
    return _headerView;
}


@end
